package com.mdd.front.service.googs.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.common.constant.GlobalConstant;
import com.mdd.common.entity.goods.Goods;
import com.mdd.common.entity.salesman.SalesmanGoogs;
import com.mdd.common.entity.salesman.Salesman;
import com.mdd.common.entity.salesman.UserSalesman;
import com.mdd.common.exception.OperateException;
import com.mdd.common.mapper.goods.GoodsMapper;
import com.mdd.common.mapper.googs.SalesmanGoogsMapper;
import com.mdd.common.mapper.salesman.SalesmanMapper;
import com.mdd.common.mapper.salesman.UserSalesmanMapper;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.googs.ISalesmanGoogsService;
import com.mdd.front.validate.PageParam;
import com.mdd.front.validate.googs.SalesmanGoogsParam;
import com.mdd.front.vo.googs.SalesmanGoogsDetailVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 推销员服务实现类
 */
@Service
@Slf4j
public class SalesmanGoogsServiceImpl extends ServiceImpl<SalesmanGoogsMapper, SalesmanGoogs> implements ISalesmanGoogsService {

    @Resource
    private UserSalesmanMapper userSalesmanMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private SalesmanMapper salesmanMapper;
    @Qualifier("salesmanGoogsMapper")
    @Autowired
    private SalesmanGoogsMapper salesmanGoogsMapper;


    /**
     * 推销员服务列表
     *
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return PageResult<SalesmanGoogsListVo>
     */
    @Override
    public List<Goods> list(PageParam pageParam, Map<String, String> params) {
        //当前用户
        Integer adminId = LikeFrontThreadLocal.getUserId();
        LambdaQueryWrapper<UserSalesman> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserSalesman::getUserId, adminId);
        lambdaQueryWrapper.eq(UserSalesman::getIsDelete, GlobalConstant.NOT_DELETE);
        UserSalesman userSalesman = userSalesmanMapper.selectOne(lambdaQueryWrapper);
        String s = params.get("id");
        Integer salesmanId = 0;
        if(Objects.nonNull(userSalesman)){
            salesmanId = userSalesman.getSalesmanId();
        }else if(StringUtils.hasText(s)){
            Integer userId = Integer.parseInt(s);
            // 根据用户id查询推销员的id
            Salesman salesman = salesmanMapper.selectOne(new LambdaQueryWrapper<Salesman>()
                    .eq(Salesman::getUserId, userId)
                    .eq(Salesman::getIsDelete, GlobalConstant.NOT_DELETE));
            if(Objects.nonNull(salesman)){
                salesmanId = salesman.getId();
            }
        } else {
            throw new OperateException("没有绑定推销员或没登录账号，不能显示页面");
        }
        LambdaQueryWrapper<SalesmanGoogs> googsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        log.info("推销员id为： {}", salesmanId);
        googsLambdaQueryWrapper.eq(SalesmanGoogs::getSalesmanId, salesmanId);
        List<SalesmanGoogs> salesmanGoogs = this.baseMapper.selectList(googsLambdaQueryWrapper);
        List<Long> collect = salesmanGoogs.stream().map(SalesmanGoogs::getProductId).collect(Collectors.toList());
        log.info("查询出来的服务id为： {}", collect);
        if(CollectionUtils.isEmpty(collect)) {
            return new ArrayList<>();
        }
        List<Goods> goods = goodsMapper.selectBatchIds(collect);
        Map<Long, BigDecimal> collect1 = salesmanGoogs.stream().collect(Collectors.toMap(SalesmanGoogs::getProductId, SalesmanGoogs::getProductPrice));
        goods.forEach(goods1 -> {
            if(collect1.containsKey(goods1.getId())){
                BigDecimal bigDecimal = collect1.get(goods1.getId());
                goods1.setPrice(bigDecimal);
            }
        });
        return goods;
    }

    /**
     * 推销员服务详情
     *
     * @param id 主键参数
     * @return SalesmanGoogs
     */
    @Override
    public SalesmanGoogsDetailVo detail(Integer id) {
        SalesmanGoogs model = this.baseMapper.selectOne(
                new QueryWrapper<SalesmanGoogs>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        SalesmanGoogsDetailVo vo = new SalesmanGoogsDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 推销员服务新增
     *
     * @param salesmanGoogsParam 参数
     */
    @Override
    public void add(SalesmanGoogsParam salesmanGoogsParam) {
        SalesmanGoogs model = new SalesmanGoogs();
        model.setSalesmanId(salesmanGoogsParam.getSalesmanId());
        model.setProductId(salesmanGoogsParam.getProductId());
        model.setProductPrice(salesmanGoogsParam.getProductPrice());
	    this.baseMapper.insert(model);
    }

    /**
     * 推销员服务编辑
     *
     * @param salesmanGoogsParam 参数
     */
    @Override
    public void edit(SalesmanGoogsParam salesmanGoogsParam) {
        SalesmanGoogs model = this.baseMapper.selectOne(
                new QueryWrapper<SalesmanGoogs>()
                    .eq("id",  salesmanGoogsParam.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(salesmanGoogsParam.getId());
        model.setSalesmanId(salesmanGoogsParam.getSalesmanId());
        model.setProductId(salesmanGoogsParam.getProductId());
        model.setProductPrice(salesmanGoogsParam.getProductPrice());
	    this.baseMapper.updateById(model);
    }

    @Override
    public List<Goods> googsList(PageParam pageParam, Map<String, String> params) {
        Integer adminId = LikeFrontThreadLocal.getUserId();
        LambdaQueryWrapper<Salesman> salesmanLambdaQueryWrapper = new LambdaQueryWrapper<>();
        salesmanLambdaQueryWrapper.eq(Salesman::getUserId, adminId);
        salesmanLambdaQueryWrapper.eq(Salesman::getIsDelete, GlobalConstant.NOT_DELETE);
        Salesman salesman = salesmanMapper.selectOne(salesmanLambdaQueryWrapper);
        Assert.notNull(salesman, "你不是一个推销员");
        LambdaQueryWrapper<SalesmanGoogs> salesmanGoogsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        salesmanGoogsLambdaQueryWrapper.eq(SalesmanGoogs::getSalesmanId, salesman.getId());
        List<SalesmanGoogs> salesmanGoogs = this.baseMapper.selectList(salesmanGoogsLambdaQueryWrapper);
        List<Long> collect = salesmanGoogs.stream().map(SalesmanGoogs::getProductId).collect(Collectors.toList());
        log.info("查询出来的服务id为： {}", collect);
        if(CollectionUtils.isEmpty(collect)) {
            return new ArrayList<>();
        }
        List<Goods> goods = goodsMapper.selectBatchIds(collect);
        Map<Long, BigDecimal> collect1 = salesmanGoogs.stream().collect(Collectors.toMap(SalesmanGoogs::getProductId, SalesmanGoogs::getProductPrice));
        goods.forEach(goods1 -> {
            if(collect1.containsKey(goods1.getId())){
                BigDecimal bigDecimal = collect1.get(goods1.getId());
                goods1.setPrice(bigDecimal);
            }
        });
        return goods;
    }


}
